package com.wdd.atcrowdfunding.manager.controller;

import com.wdd.atcrowdfunding.bean.Cert;
import com.wdd.atcrowdfunding.bean.Member;
import com.wdd.atcrowdfunding.manager.service.CertService;
import com.wdd.atcrowdfunding.potal.service.MemberCertService;
import com.wdd.atcrowdfunding.potal.service.MemberService;
import com.wdd.atcrowdfunding.util.AjaxResult;
import com.wdd.atcrowdfunding.util.Page;
import com.wdd.atcrowdfunding.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实名认证审核
 */

@Controller
@RequestMapping("/auth_cert")
public class authCertController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private CertService certService;
    @Autowired
    private MemberCertService memberCertService;


    @RequestMapping("/index.htm")
    public String toIndex(){
        return "memberReview/auth_cert";
    }


    /**
     * 查看详细资料
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/selectDetail.htm")
    public String selectDetail(Integer id, Model model){

        Member member = memberService.selectById(id);
        List<Cert> certs = certService.queryCertByAccttype(member.getAccttype());
        model.addAttribute("member",member);
        model.addAttribute("certs",certs);

        return "memberReview/selectDetail";
    }


    /**
     * 异步加载页面
     * @param pageno
     * @param pagesize
     * @param queryText
     * @return
     */
    @RequestMapping("/doIndex")
    @ResponseBody
    public AjaxResult doIndex(@RequestParam(value = "pageno",required = false,defaultValue = "1") Integer pageno,
                              @RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize,
                              String queryText){

        AjaxResult ajaxResult = new AjaxResult();
        try{
            Map<String,Object> paramMap = new HashMap();
            paramMap.put("pageno",pageno);
            paramMap.put("pagesize",pagesize);
            if(StringUtil.isNotEmpty(queryText)){
                if (queryText.contains("%")){
                    queryText = queryText.replaceAll("%","\\\\%");
                }
                if (queryText.contains("_")){
                    queryText = queryText.replaceAll("_","\\\\_");
                }
                paramMap.put("queryText",queryText);
            }
            Page<Member> page = memberService.queryMemberPage(paramMap);
            ajaxResult.setSuccess(true);
            ajaxResult.setPage(page);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("查询数据失败");
        }
        return ajaxResult;
    }

    /**
     * 查看相关图片
     * @return
     */
    @RequestMapping("/showImg")
    @ResponseBody
    public AjaxResult showImg(Integer certid){

        AjaxResult ajaxResult = new AjaxResult();
        try{
            String img = memberCertService.selectImgByCid(certid);
            ajaxResult.setImg(img);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("图片显示失败");
        }
        return ajaxResult;
    }


    /**
     * 审核通过
     * @param id
     * @return
     */
    @RequestMapping("/pass")
    @ResponseBody
    public AjaxResult pass(Integer id){

        AjaxResult ajaxResult = new AjaxResult();
        try{
            Member member = new Member();
            member.setAuthstatus("2");
            member.setId(id);
            memberService.updateAuthstatus(member);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    /**
     * 实名认证未通过
     * @param id
     * @return
     */
    @RequestMapping("/dispass")
    @ResponseBody
    public AjaxResult dispass(Integer id){

        AjaxResult ajaxResult = new AjaxResult();
        try{
            Member member = new Member();
            member.setAuthstatus("3");
            member.setId(id);
            memberService.updateAuthstatus(member);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

}
